/**
 * 
 */
package com.wbcs.system.component.application.report;

import java.util.LinkedHashMap;

import com.jf.json.JbsfJsonFactory;
import com.wbcs.config.Config;
import com.wbcs.config.component.IComponentConfigBean;
import com.wbcs.config.component.application.report.ColBean;
import com.wbcs.config.component.application.report.ReportBean;
import com.wbcs.config.component.container.page.PageBean;
import com.wbcs.exception.WbcsRuntimeException;
import com.wbcs.system.ReportRequest;
import com.wbcs.system.component.container.AbsContainerType;
import com.wbcs.system.inputbox.PopUpBox;
import com.wbcs.system.tags.component.AbsComponentTag;

/**
 * 帮助页面主体类
 * @author hawkfly
 * @byDate 201908302052
 */
public class HelpListReportType extends EditableListReportType2 {
	
	private String pageid;
	private String reportid;
	private String fromPropertyNames;
	private String inputboxid;
	private final String SIGN = "help";
	private final String BREAK = ";";
	private final String BREAK2 = "=";
	
	public HelpListReportType(AbsContainerType parentContainerType,
			IComponentConfigBean comCfgBean, ReportRequest rrequest) {
		
		super(parentContainerType, comCfgBean, rrequest);
		// TODO Auto-generated constructor stub
		if(rrequest != null)
		{
			pageid=rrequest.getStringAttribute("SRC_PAGEID");
	        reportid=rrequest.getStringAttribute("SRC_REPORTID");
	        inputboxid=rrequest.getStringAttribute("INPUTBOXID");
		}
	}

	@Override
	public void displayOnPage(AbsComponentTag displayTag)
	{
		 super.displayOnPage(displayTag);
		 if(inputboxid == null || pageid == null || reportid == null)return;
		 String boxid=inputboxid;
         int idx=boxid.lastIndexOf("__");
         if(idx>0)
         {
            boxid=boxid.substring(0,idx);
         }
         ReportBean rbean = Config.getInstance().getPageBean(pageid).getReportChild(reportid, true);
		 PopUpBox popupbox = rbean.getPopUpBox(boxid);
		 if(popupbox==null)
         {
            throw new WbcsRuntimeException("报表"+rbean.getPath()+"下面不存在ID为"+inputboxid+"的文件上传输入框");
         }
		 wresponse.println(popupbox.createSelectOkFunction(inputboxid));
		 
		 FromTo[] paramsjson = parseHelpParamValue(popupbox.getParam()); //赋值映射关系
		 PageBean pagebean = this.getReportBean().getPageBean();
		 //JavaScriptAssistant.getInstance().writeJsMethodToJsFiles(pagebean, "");
		 LinkedHashMap<String, Object> paraMap = new LinkedHashMap<String, Object>();
		 paraMap.put("pageid", pagebean.getId());
		 paraMap.put("reportid", this.getReportBean().getId());
		 paraMap.put("srcpageid", pageid);
		 paraMap.put("srcreportid", reportid);
		 paraMap.put("mapRelation",paramsjson);
		 if(fromPropertyNames == null || fromPropertyNames.equals(""))
		 {
			 wresponse.getMessageCollector().error("弹出页面取数关系没有设定，请联系管理员！", true);
			 return;
		 }
		 paraMap.put("fromPropertyNames", fromPropertyNames.split(","));
		 String methodparams = JbsfJsonFactory.me().getJson().toJson(paraMap);
		 
		 rrequest.getWResponse().addCallbackMethod("fixedRowColTable", "autoBindTrDbclick", methodparams);
	}
	
	public FromTo[] parseHelpParamValue(String srcparams){
		if(srcparams == null)
		{
			throw new WbcsRuntimeException("调用Help报表时没有取得赋值映射关系！");
		}
		String[] params = srcparams.split(BREAK);
		if(params.length == 0)
		{
			throw new WbcsRuntimeException("调用Help报表时没有设定赋值映射关系！");
		}
		
		FromTo[] arrayResult = new FromTo[params.length];
		for (int i = 0; i < params.length; i++) 
		{
			String[] evalExpression = params[i].split(BREAK2);//F_DMNOTE=help{short_name}
			FromTo fromto = new FromTo();
			fromto.setFrom(rmHelpSign(evalExpression[1]));
			fromto.setTo(evalExpression[0]);
			
			PageBean pbean = Config.getInstance().getPageBean(pageid);
			ColBean cbean = pbean.getReportChild(reportid, true).getDbean().getColBeanByColColumn(evalExpression[0]);
			if(cbean != null)fromto.setTotype(cbean.getDisplaytype());
			
			arrayResult[i] = fromto;
			
			if(fromPropertyNames == null) fromPropertyNames = "";
			fromPropertyNames = 
					fromPropertyNames.trim().lastIndexOf(",") != -1 || fromPropertyNames.trim().equals("") 
					? fromPropertyNames+fromto.getFrom() : fromPropertyNames + "," + fromto.getFrom(); 
		}
		
		return arrayResult;
	}
	
	private String rmHelpSign(String expression) {
		// TODO Auto-generated method stub
		if(expression == null)
		{
			throw new WbcsRuntimeException("HelpReportType设置的help{XXX}表达式为空！请检查配置！");
		}
		
		return expression.replace(SIGN, "").replace("{", "").replace("}", "");
	}

	public class FromTo
	{
		private String from;
		private String to;
		private String totype;
		
		public String getFrom() {
			return from;
		}
		public void setFrom(String from) {
			this.from = from;
		}
		public String getTo() {
			return to;
		}
		public void setTo(String to) {
			this.to = to;
		}
		public String getTotype() {
			return totype;
		}
		public void setTotype(String totype) {
			this.totype = totype;
		}
	}
}
